package lxy.contacttest;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

import lxy.contacttest.utils.EmailBean;
import lxy.contacttest.utils.PhoneBean;
import lxy.contacttest.utils.PhoneContactUtils;


/**
 * Created by dengdayi
 * Date: 2016/06/22 14：55
 */
public class ContactDetailActivity extends AppCompatActivity {

    private EditText mEtPhone;
    private EditText mEtEmail;
    private TextView mTitleTextView;

    private String mContactId;

    private String contactName;


    private int phoneDataId = -1;
    private int emailDataId = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);
        setUpView();
    }

    /**
     * 初始化View及设置Data
     */
    private void setUpView() {
        mEtPhone = (EditText) findViewById(R.id.et_mobile);
        mEtEmail = (EditText) findViewById(R.id.et_email);

        mTitleTextView = (TextView) findViewById(R.id.tv_name);

        mContactId = getIntent().getStringExtra("contact_id");
        contactName = getIntent().getStringExtra("name");
        mTitleTextView.setText(contactName);
        //这里我修改手机和邮箱的第一条数据
        List<PhoneBean> phoneBeans = PhoneContactUtils.getContactPhoneList(this, mContactId);
        if (phoneBeans != null && !phoneBeans.isEmpty()) {
            phoneDataId = phoneBeans.get(0).getId();
            mEtPhone.setText(phoneBeans.get(0).getNumber());
        }

        List<EmailBean> emailBeans = PhoneContactUtils.getContactEmailList(this, mContactId);
        if (emailBeans != null && !emailBeans.isEmpty()) {
            emailDataId = emailBeans.get(0).getId();
            mEtEmail.setText(emailBeans.get(0).getEmail());
        }
    }

    /**
     * 更新操作
     *
     * @param view
     */
    public void doUpdate(View view) {
        String phone = mEtPhone.getText().toString().trim();
        String email = mEtEmail.getText().toString().trim();
        //更行操作需放入异步中处理
        new UpdateTask(phone, email).execute();
    }

    /**
     * 删除操作
     *
     * @param view
     */
    public void doDelete(View view) {
        PhoneContactUtils.deleteContact(this, mContactId);
        setResult(RESULT_OK);
        finish();
    }

    private class UpdateTask extends AsyncTask<Void, Integer, Void> {
        private String phone;
        private String email;

        public UpdateTask(String phone, String email) {
            this.phone = phone;
            this.email = email;
        }

        @Override
        protected Void doInBackground(Void... params) {

            PhoneContactUtils.updateUserMobile(ContactDetailActivity.this, mContactId, phoneDataId, phone);
            PhoneContactUtils.updateUserEmail(ContactDetailActivity.this, mContactId, emailDataId, email);
            return null;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            Toast.makeText(ContactDetailActivity.this, "更新成功", Toast.LENGTH_LONG).show();
            setResult(RESULT_OK);
            finish();
        }
    }
}
